package com.zb.web;

import com.zb.pojo.Cart;
import com.zb.pojo.Order;
import com.zb.pojo.OrderItem;
import com.zb.pojo.User;
import com.zb.service.OrderService;
import com.zb.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zb
 * @create 2021-03-09 10:58
 */
public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 查看所有订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询全部订单
        List<Order> orders = orderService.showAllOrders();
        //保存到request域中
        req.setAttribute("orders",orders);
        //请求转发到/pages/manager/order_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(req,resp);
    }

    /**
     * 发货
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取订单编号
        String orderId = req.getParameter("orderId");
        //订单发货
        orderService.sendOrder(orderId);
        //重定向到之前的页面
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showAllOrders");
    }

    /**
     * 查看订单详情
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户的姓名
        User user = (User) req.getSession().getAttribute("user");
        String username = user.getUsername();

        //获取订单编号
        String orderId = req.getParameter("orderId");
        //查询订单信息
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //保存到request域中
        req.setAttribute("orderItems",orderItems);

        //判断user是不是管理员
        if("admin".equals(username)){
            //请求转发到/pages/manager/order_item_manager.jsp
            req.getRequestDispatcher("/pages/manager/order_item_manager.jsp").forward(req,resp);
        }else{
            //请求转发到/pages/order/order_item.jsp
            req.getRequestDispatcher("/pages/order/order_item.jsp").forward(req,resp);
        }
    }


    /**
     * 查看我的订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取用户的userId
        User user = (User)req.getSession().getAttribute("user");
        Integer userId = user.getId();
        //查询我的订单
        List<Order> orders = orderService.showMyOrders(userId);
        //将其保存到request域中
        req.setAttribute("orders",orders);
        //请求转发到/pages/order/order.jsp
        req.getRequestDispatcher("/pages/order/order.jsp").forward(req,resp);
    }

    /**
     * 点击收货
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiverOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取订单编号
        String orderId = req.getParameter("orderId");
        //调用orderService.receiverOrder()方法签收订单
        orderService.receiverOrder(orderId);
        //重定向到之前的页面
        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showMyOrders");
    }
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取cart购物车对象
        Cart cart = (Cart)req.getSession().getAttribute("cart");
        //获取userId
        User loginUser = (User)req.getSession().getAttribute("user");

        if(loginUser == null){
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
            return ;
        }

        System.out.println("OrderServlet程序在【"+Thread.currentThread()+"】");

        Integer userId = loginUser.getId();
        //调用orderService.createOrder(cart,userId);生成订单;(createOrder中已将购物车清空,无需再处理)
        String orderId = orderService.createOrder(cart,userId);

        //使用重定向
        req.getSession().setAttribute("orderId",orderId);
        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");

    }
}
